package com.topvision.jack.camerasdk2.camera;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Point;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.topvision.jack.camerasdk2.R;
import com.topvision.jack.camerasdk2.camera.utils.CameraUtils;

import java.io.File;
import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2017/12/25.
 */

public class PhotoLoaderTask extends AsyncTask<String, Void, String> {
    private final WeakReference<ImageView> imageViewReference;
    //目标文件地址
    private String data = null;
    Context context;
    Point point;


    public PhotoLoaderTask(Context context, ImageView imageView) {
        imageViewReference = new WeakReference<>(imageView);
        this.context = context;
        point = CameraUtils.getScreenPoint(context);
    }

    // Decode image in background.
    @Override
    protected String doInBackground(String... params) {
        data = params[0];
        return data;
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @SuppressLint("NewApi")
    @Override
    protected void onPostExecute(String data) {

        File imageFile = new File(data);
        if (imageFile != null && imageFile.exists() && imageFile.length() > 0) {
            Picasso.with(context)
                    .load(imageFile)
                    .error(R.mipmap.pic_fail)
                    .resize(point.x + 2 * context.getResources().getDimensionPixelSize(R.dimen.image_shift) * 2, point.y + 2 * context.getResources().getDimensionPixelSize(R.dimen.image_shift) * 2)
                    .transform(new MyTransformation(imageViewReference.get()))
                    .into(imageViewReference.get(), new ShowPhotoCallback());
        }
    }

    class ShowPhotoCallback implements Callback {

        @Override
        public void onSuccess() {

        }

        @Override
        public void onError() {

        }
    }

}
